<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <ul id="list">
      <li>
        li1
        <ul>
          <li>l11</li>
          <li>
            l12
            <ul>
              <li>l121</li>
              <li>l122</li>
              <li>l123</li>
              <li>l124</li>
            </ul>
          </li>
          <li>l13</li>
        </ul>
      </li>
      <li>
        li2
        <ul>
          <li>l21</li>
          <li>l22</li>
        </ul>
      </li>
      <li>li3</li>
    </ul>
    <script>
      function treeList(ul) {
        [...ul.children].forEach((node) => {
          let count = 0;
          if (node.children[0] !== undefined) {
            count = node.children[0].children.length;
            // 继续统计(递归)
            treeList(node.children[0]);
            node.childNodes[0].data += `【${count}】`;
          }
        });
      }

      treeList(document.getElementById("list"));
    </script>
  </body>
</html>
